package StudyCode.summary.textfile;

import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class WritingDataTextFileWithFormatter {
	private static Formatter output;

	public static void main(String[] args) {
		openFile();
		addRecords();
		closeFile();
	}

	public static void openFile() {
		try {
			output = new Formatter("E:/Copy/test1.txt");
		} catch (SecurityException securityException) {
			securityException.printStackTrace();
		} catch (FileNotFoundException fileNotFoundException) {
			fileNotFoundException.printStackTrace();
		}
	}

	public static void addRecords() {
		Scanner input = new Scanner(System.in);
		System.out.printf("%s%n%s%n? ", "输入帐号，名字，姓氏和余额", "输入文件结束指示符以结束输入.");

		while (input.hasNext()) {
			try {
				output.format("%d %s %s %.2f%n", input.nextInt(), input.next(),
						input.next(), input.nextDouble());
			} catch (FormatterClosedException formatterClosedException) {
				formatterClosedException.printStackTrace();
				break;
			} catch (NoSuchElementException elementException) {
				elementException.printStackTrace();
				input.nextLine();
			}

			System.out.print("? ");
		}
	}

	public static void closeFile() {
		if (output != null)
			output.close();
	}
}
